//
//  LoginVC.m
//  BasePorject_App
//
//  Created by Yutian Duan on 2019/5/27.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "LoginVC.h"
#import "BaseNavigationVC.h"
#import "RegistVC.h"
#import "ForgetVC.h"
#import "YFBaseTextField.h"

#define TextFieldFont 13.5f


@interface LoginVC () {
  
  UIImageView *remenberImageView;
  
  ///! 记录当前视图的状态栏颜色是否是白色
  BOOL isWhiteStates;
  
}


@property (nonatomic, strong) YFBaseTextField *phoneField;
@property (nonatomic, strong) YFBaseTextField *passwordField;
///! 是否记住密码
@property (nonatomic, strong) UIButton *remenberButton;

@end

@implementation LoginVC


- (void)viewDidLoad {
  [super viewDidLoad];
  
  
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:animated];
  if (isWhiteStates) {
    [self setStatusBarStyleWhite];
  } else {
    [self setStatusBarStyleBack];
  }
}


- (void)createFields {
  [super createFields];

  self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];

  if ([UIApplication sharedApplication].statusBarStyle == UIStatusBarStyleLightContent) {
    isWhiteStates = YES;
  } else {
    isWhiteStates = NO;
  }
  
}

- (void)createViews {
  [super createViews];

  
  ///!
  CGFloat imageWith = ScreenWidth - 40;
  
  UIView *contenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, imageWith , imageWith)];
  contenView.backgroundColor = HEXCOLOR(0x292929);
  contenView.layer.cornerRadius = 8.0f;
  contenView.center = CGPointMake(ScreenWidth/2, (ScreenHeight - TabBarHeight)/2);
  [self.view addSubview:contenView];
  
  CGFloat cancelH = 28;
  CGFloat logoH = 13;
  CGFloat fontOffset = 0;
  CGFloat leftOffset = 0;
  CGFloat labelW = 80;
  if (IS_IPHONE5) {
    cancelH = 24;
    logoH = 11;
    fontOffset = 2;
    leftOffset = 20;
    labelW = 70;
  }
  
  ///! 取消
  UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, cancelH, cancelH)];
  [cancelButton setBackgroundImage:[UIImage imageNamed:@"Logoin_cancel"] forState:UIControlStateNormal];
  [cancelButton addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
  cancelButton.yt_right = contenView.yt_width - 20;
  [contenView addSubview:cancelButton];
  
  
  
  //! logo
  UIImageView *logoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, cancelButton.yt_bottom, logoH * 14, logoH)];
  logoView.image = [UIImage imageNamed:@"Login_logo"];
  logoView.contentMode = UIViewContentModeScaleAspectFit;
  [contenView addSubview:logoView];
  logoView.yt_centerX = contenView.yt_width/2;
  
  
  UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, logoView.yt_bottom + 20, contenView.yt_width - 80, 30)];
  titleLabel.text = YTLocalizedString(@"会员登入");
  titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18.0f - fontOffset];
  titleLabel.textAlignment = NSTextAlignmentCenter;
  titleLabel.textColor = [UIColor whiteColor];
  [contenView addSubview:titleLabel];
  
  
  
  UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 - leftOffset, titleLabel.yt_bottom + 20, labelW, 35)];
  phoneLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"手机号码")];
  phoneLabel.textColor = [UIColor whiteColor];
  phoneLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  phoneLabel.textAlignment = NSTextAlignmentLeft;
  [contenView addSubview:phoneLabel];
  

  _phoneField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(phoneLabel.yt_right, phoneLabel.yt_top, contenView.yt_width - phoneLabel.yt_right - labelW/2 + leftOffset, phoneLabel.yt_height)];
  [_phoneField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_phoneField set_placeTextColor:DEF_GRAY];
  _phoneField.placeholder = YTLocalizedString(@"输入已登记手机号码");
  _phoneField.layer.masksToBounds = YES;
  _phoneField.layer.cornerRadius = 8;
  _phoneField.backgroundColor = [UIColor whiteColor];
  _phoneField.leftViewMode = UITextFieldViewModeAlways;
  _phoneField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [contenView addSubview:_phoneField];
  
  
  UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.yt_left, phoneLabel.yt_bottom + 20, phoneLabel.yt_width, phoneLabel.yt_height)];
  passwordLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"登录密码")];
  passwordLabel.textColor = [UIColor whiteColor];
  passwordLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  passwordLabel.textAlignment = NSTextAlignmentLeft;
  [contenView addSubview:passwordLabel];
  
  _passwordField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_phoneField.yt_left, passwordLabel.yt_top, _phoneField.yt_width, _phoneField.yt_height)];
  [_passwordField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_passwordField set_placeTextColor:DEF_GRAY];
  _passwordField.placeholder = YTLocalizedString(@"输入登录密码");

  _passwordField.layer.masksToBounds = YES;
  _passwordField.layer.cornerRadius = 8;
  _passwordField.backgroundColor = [UIColor whiteColor];
  _passwordField.leftViewMode = UITextFieldViewModeAlways;
  _passwordField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [contenView addSubview:_passwordField];
  
  
//  CGFloat imageH = 21;
  CGFloat imageTop = 10;
  if (IS_IPHONE5) {
//    imageH = 15;
    imageTop = 5;
  }
  
  
  UIImageView *unRemenberImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_passwordField.yt_left, _passwordField.yt_bottom + imageTop, 15, 15)];
  unRemenberImageView.image = [UIImage imageNamed:@"Login_unRemenber"];
  unRemenberImageView.contentMode = UIViewContentModeScaleAspectFit;
  [contenView addSubview:unRemenberImageView];
  
  //
  remenberImageView = [[UIImageView alloc] initWithFrame:unRemenberImageView.frame];
  remenberImageView.image = [UIImage imageNamed:@"Login_remenber"];
  remenberImageView.contentMode = UIViewContentModeScaleAspectFit;
  [contenView addSubview:remenberImageView];
  remenberImageView.hidden = YES;
  
  _remenberButton = [[UIButton alloc] initWithFrame:CGRectMake(unRemenberImageView.yt_right + 2, unRemenberImageView.yt_top - 1.5, 100, 18)];
  [_remenberButton setTitle:YTLocalizedString(@"保持登入状态") forState:UIControlStateNormal];
  _remenberButton.titleLabel.font = [UIFont systemFontOfSize:12.0f - fontOffset];
  [_remenberButton setTitleColor:DEF_GRAY forState:UIControlStateNormal];
  _remenberButton.selected = YES;
  [_remenberButton addTarget:self action:@selector(remenberClick:) forControlEvents:UIControlEventTouchUpInside];
  [_remenberButton sizeToFit];
  [contenView addSubview:_remenberButton];
  _remenberButton.yt_centerY = unRemenberImageView.yt_centerY;
  
  ///! 忘记密码
  UIButton *forgetButton = [[UIButton alloc] initWithFrame:CGRectMake(_passwordField.yt_right - _remenberButton.yt_width, _remenberButton.yt_top, _remenberButton.yt_width, _remenberButton.yt_height)];
  forgetButton.titleLabel.font = [UIFont systemFontOfSize:12.0f - fontOffset];
  forgetButton.titleLabel.textAlignment = NSTextAlignmentRight;
  [forgetButton setTitleColor:DEF_GRAY forState:UIControlStateNormal];
  [forgetButton addTarget:self action:@selector(forgetClick) forControlEvents:UIControlEventTouchUpInside];
  [contenView addSubview:forgetButton];
  
  NSDictionary *styleDic = @{
                             NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle),
                             NSForegroundColorAttributeName:DEF_GRAY
                             };
  
  NSString *forgetStr = [NSString stringWithFormat:@"%@?",YTLocalizedString(@"忘记密码")];
  NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc] initWithString:forgetStr attributes:styleDic];
  [forgetButton setAttributedTitle:attribtStr forState:UIControlStateNormal];

  
  
  ///! regist
  UIButton *registButton = [[UIButton alloc] initWithFrame:CGRectMake(0, _remenberButton.yt_bottom + 20 - leftOffset, 123, 25)];
  registButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  registButton.backgroundColor = DEF_GRAY;
  [registButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [registButton setTitle:YTLocalizedString(@"注册") forState:UIControlStateNormal];
  registButton.layer.masksToBounds = YES;
  registButton.layer.cornerRadius = 8;
  registButton.yt_right = contenView.yt_width/2 - 5;
  [registButton addTarget:self action:@selector(registClick) forControlEvents:UIControlEventTouchUpInside];
  [contenView addSubview:registButton];

  //
  UILabel *registTipLabel = [[UILabel alloc] initWithFrame:CGRectMake(registButton.yt_left, registButton.yt_bottom + 10, registButton.yt_width, 12)];
  registTipLabel.textAlignment = NSTextAlignmentCenter;
  registTipLabel.text = [NSString stringWithFormat:@"%@？",YTLocalizedString(@"还没有成为HBC会员")];
  registTipLabel.font = [UIFont systemFontOfSize:11.0f - fontOffset];
  registTipLabel.textColor = DEF_GRAY;
  [contenView addSubview:registTipLabel];

  
  ///!
  UIButton *loginButton = [[UIButton alloc] initWithFrame:CGRectMake(registButton.yt_right + 10, registButton.yt_top, registButton.yt_width, registButton.yt_height)];
  loginButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  loginButton.backgroundColor = DEF_RED;
  [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [loginButton setTitle:YTLocalizedString(@"登入") forState:UIControlStateNormal];
  loginButton.layer.masksToBounds = YES;
  loginButton.layer.cornerRadius = 8;
//  [loginButton setBackgroundImage:[UIImage imageNamed:@"Login_login"] forState:UIControlStateNormal];
  [loginButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
  [contenView addSubview:loginButton];
  
}

- (void)createConfig {
  [super createConfig];
  if ([AppUser phone].length > 0) {
    _phoneField.text = [AppUser phone];
  }
  
  if ([AppUser password].length > 0) {
    _passwordField.text = [AppUser password];
  }
  
}

- (void)cancelClick {
  [self.view endEditing:YES];
  [self goBackVC];
}

- (void)remenberClick:(UIButton *)sender {
  
  sender.selected = !sender.selected;
  if (sender.selected) {
    remenberImageView.hidden = NO;
  } else {
    remenberImageView.hidden = YES;
  }
  [AppUser setAutLogin:sender.selected];
  
}

- (void)forgetClick {
  ForgetVC *forgetVC = [[ForgetVC alloc] init];
  BaseNavigationVC *forgetNav = [[BaseNavigationVC alloc] initWithRootViewController:forgetVC];
  [self presentViewController:forgetNav animated:YES completion:nil];

}

- (void)registClick {
  
  RegistVC *registVC = [[RegistVC alloc] init];
  BaseNavigationVC *registNav = [[BaseNavigationVC alloc] initWithRootViewController:registVC];
  [self presentViewController:registNav animated:YES completion:nil];
  
  __weak typeof(self) weakSelf = self;
  registVC.registSuccessed = ^{
    ///!
    weakSelf.phoneField.text = [AppUser phone];
  };
  
}

- (void)loginClick {
  
  if ([self checkEnabel] == NO) {
    return;
  }
  
  __weak typeof(self) weakSelf = self;
  [[ToolsServer sharedInstance] loginWithAccount:_phoneField.text password:_passwordField.text finished:^(BOOL finish) {
    __strong typeof(weakSelf) strongSelf = weakSelf;
    if (finish) {
      ///! 登录成功
      [[NSNotificationCenter defaultCenter] postNotificationName:NOTICE_LOGIN_APP object:nil];
      [strongSelf goBackVC];
      if (strongSelf.loginSuccessed) {
        strongSelf.loginSuccessed();
      }
    }
    
  } failed:^(NSError *error) {
    
  }];
  
}

//! 校验
- (BOOL)checkEnabel {
  if (_phoneField.text.length == 0) {
    [SVProgressHUD showErrorWithStatus:YTLocalizedString(@"请输入手机号码")];
    return NO;
  } else if (_passwordField.text.length == 0) {
    [SVProgressHUD showErrorWithStatus:YTLocalizedString(@"请输入密码")];
    return NO;
  }
  return YES;
}

@end
